home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5555 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  61 lines

  1. Path: nntp.coast.net!torn!nott!cunews!usenet
  2. From: tcope@chat.carleton.ca (Tyler Cope)
  3. Newsgroups: comp.lang.c
  4. Subject: memory allocation using malloc and free
  5. Date: Mon, 19 Feb 1996 17:36:47 GMT
  6. Organization: Carleton University
  7. Message-ID: <4gagll$5rc@bertrand.ccs.carleton.ca>
  8. Reply-To: tcope@chat.carleton.ca
  9. NNTP-Posting-Host: alpha02.scs.carleton.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Could someone please demonstrate the proper (or _a_ proper) ANSI
  13. method of allocating >64K space using malloc and free. I've tried many
  14. versions and most work when malloc() and free() are both called from
  15. main(), but when a separate function is called, everything goes crazy.
  16.  
  17. To me, I don't see any reason why the following does not work, but it
  18. doesn't... it hangs up on me.
  19.  
  20. #include <process.h>
  21. #include <stdio.h>
  22. #include <alloc.h>
  23.  
  24. int main(void)
  25. {
  26.         void get_mem(unsigned char far *);
  27.         void free_mem(unsigned char far *);
  28.  
  29.         unsigned char far *ptr;
  30.  
  31.         get_mem(ptr);
  32.         freemem(ptr);
  33.  
  34.         return(1);
  35. }
  36.  
  37. void get_mem(unsigned char far *fptr)
  38. {
  39.         if ((fptr = (unsigned char far *)malloc(320*200+1)) == NULL)
  40.         {
  41.                 printf("Could not allocate enough memory");
  42.                 exit(1);
  43.         }
  44. }
  45.  
  46. void freemem(unsigned char far *fptr)
  47. {
  48.         /*  I think the problem may be here.
  49.              I've tried farfree((void far *)fptr); here as well, which
  50.      is availible to me because I'm using Borland Turbo C++ but i'd
  51. like
  52.      to be as portable as possible. free() takes a near pointer right? so 
  53.     how do you free a far pointer using a function that requires a near
  54.     pointer?  */
  55.         free((void far *)fptr);
  56. }
  57.  
  58. tcope@chat.carleton.ca
  59.  
  60.  
  61.